package v1

import (
	"bc/service"
	. "bc/util"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

type rule struct {

}
func (r *rule)Create(c *gin.Context){

	d := map[string]interface{}{
		"c_time":time.Now().Unix(),
	}
	if v,ok := c.GetPostForm("name");ok{
		d["name"]  = v
	}else{
		c.JSON(200,gin.H{
			"code":ERROR_CODE,
			"msg":ERROR,
		})
		return
	}
	if v,ok := c.GetPostForm("icon");ok{
		d["icon"]  = v
	}else{
		c.JSON(200,gin.H{
			"code":ERROR_CODE,
			"msg":"err",
			"data":"icon必填",
		})
		return
	}

	if v,ok := c.GetPostForm("desc");ok{
		d["desc"] = v
	}
	if v,ok := c.GetPostForm("path");ok{
		d["path"] = v
	}else{
		c.JSON(200,gin.H{
			"code":ERROR_CODE,
			"msg":ERROR,
		})
		return
	}

	b,id := service.Rule.Create(d)
	if b == true{

		LogAdd(c,"添加权限" + strconv.Itoa(id),"权限用户")

		c.JSON(200,gin.H{
			"code":SUCCESS_CODE,
			"msg":SUCCESS,
		})
		return
	}

	c.JSON(200,gin.H{
		"code":ERROR_CODE,
		"msg":ERROR,
	})
}

func (r *rule) Delete(c *gin.Context){
	if c.Param("ru_id") == ""{
		c.JSON(200,gin.H{
			"code":500,
			"msg":"请选择",
		})
		return
	}

	rid,_ := strconv.Atoi(c.Param("ru_id"))
	d := map[string]interface{}{
		"ru_id" :  rid,
	}

	b := service.Rule.Delete(d)

	if b == true{
		c.JSON(200,gin.H{
			"code":200,
			"msg":"操作成功",
		})
		return
	}
	c.JSON(200,gin.H{
		"code":500,
		"msg":"失败",
	})
}
func (r *rule)Update(c *gin.Context){

	if c.Param("ru_id") == ""{
		c.JSON(200,gin.H{
			"code":500,
			"msg":"ru_id is must",
		})
		return
	}

	w := map[string]interface{}{
		"ru_id":c.Param("ru_id"),
	}
	fs := map[string]interface{}{
	}
	if v,ok := c.GetPostForm("name");ok{
		fs["name"] = v
	}
	if v,ok := c.GetPostForm("icon");ok{
		fs["icon"] = v
	}
	if v,ok := c.GetPostForm("desc");ok{
		fs["desc"] = v
	}
	if v,ok := c.GetPostForm("path");ok{
		fs["path"] = v
	}

	b := service.Rule.Update(w,fs)
	if b == true{
		c.JSON(200,gin.H{
			"code":200,
			"msg":"更改成功",
		})
		return
	}
	c.JSON(200,gin.H{
		"code":500,
		"msg":"error",
	})
}

func (r *rule)First(c *gin.Context){

	rid,err:= strconv.Atoi(c.Param("ru_id"))
	if err != nil {
		c.JSON(500,gin.H{
			"code":200,
			"msg":"参数错误",
		})
		return
	}

	w := map[string]interface{}{
		"ru_id": rid,
	}

	f := []string{"ru_id","name","icon", "desc","path","c_time","d_time"}
	b,d := service.Rule.First(w,f)
	if b == true{
		c.JSON(200,gin.H{
			"code":200,
			"msg":"操作成功",
			"data":d,
		})
		return
	}
	c.JSON(500,gin.H{
		"code":500,
		"msg":"error",
		"data":"",
	})
}
func (r *rule) Find(c *gin.Context){

	p,_ := strconv.Atoi(c.DefaultQuery("page","1"))
	pageSize := c.DefaultQuery("pageSize",strconv.FormatInt(Config.App.PageSize,10))
	ps,_ := strconv.Atoi(pageSize)

	w := map[string]interface{}{

	}
	if c.Query("name") != ""{
		w["name"] = c.Query("name")
	}

	d,total,err := service.Rule.Find(w,p,ps)
	if err !=nil{
		c.JSON(500,gin.H{
			"code":500,
			"msg":"error",
		})
		return
	}
	c.JSON(200,gin.H{
		"code":200,
		"msg":"操作成功",
		"data":d,
		"total":total,
	})
}

var Rule = &rule{}
